﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TDSServerLib;

namespace ServerTest{
    
    public partial class UserGraph01 : Form
    {
        public UserGraph01()
        {
            InitializeComponent();
        }




        public string UserNameLabel
        {
            set
            {
                this.UserName.Text = value;
            }
        }

        public string MachineLabel
        {
            set
            {
                this.Machine.Text = value;
            }
        }

        public string WeightLabel
        {
            set
            {
                this.WeightValue.Text = value;
            }
        }

        public string TimeLabel
        {
            set
            {
                this.TimeValue.Text = value;
            }
        }

        public string RepLabel
        {
            set
            {
                this.RepValue.Text = value;
            }
        }

        
        public delegate double Function(double x); //declare a delegate that takes and returns double 


        public static double f1(double x)
        {
            return x;
        }

        public static double integral(byte[] _data, double a, double b, int step_number)
        {
            double sum = 0;
            double step_size = (b - a) / step_number;
            for (int i = 0; i < step_number; i = i + 2) //Simpson algorithm samples the integrand in several point which significantly improves //precision.
                sum = sum + ((double)_data[(int)a + i * (int)step_size] + 4 * (double)_data[(int)a + (i + 1) * (int)step_size] + (double)_data[(int)a + (i + 2) * (int)step_size]) * step_size / 3; //divide the area under f(x)     //into step_number rectangles and sum their areas 
            return sum;
        }
        
        public double derivative(byte[] _data, int x){
            int h  = 1;
            int h2 = 2 * h;

            try
            {
                return ((double)_data[x + h] - (double)_data[x]) / h;
            }
            catch{
                return 0.0;
            }
        }

        public double[] derivativeDouble(double[] _data)
        {
            double[] _result = new double[_data.Length];
            int h = 1;
            int h2 = 2 * h;
            for (int i = 0; i < _data.Length; i++)
            {
                try{
                    _result[i] = (_data[i + h] - _data[i]) / h;
                }
                catch
                {
                    _result[i] = 0.0;
                }
            }
            return _result;
        }

        public int Repeticiones(double[] _data)
        {
            
            int r = 0;
            for (int i = 1; i < _data.Length; i ++){
                try
                {
                     if ((_data[i] < 0.0) && (_data[i-1] >= 0.0))
                         r++;
                }
                catch
                {
                   
                }
            }
            return r;
        }

        public double[] LowPassFilter(double[] _data){
            double[] _result = new double[_data.Length];
           
            for (int i = 0; i < _data.Length; i++)
            {
                try
                {
                    _result[i] = _data[i] + _data[i-1];
                }
                catch
                {
                    _result[i] = 0.0;
                }
            }
            return _result;
        }

        public double derivativeEval(double _data)
        {
            int h = 1;
            int h2 = 2 * h;

            try
            {
                return ((_data + h) - _data) / h;
            }
            catch
            {
                return 0.0;
            }
        }

        public byte[] derivativeArray(byte[] _data)
        {
            var _result = new MemoryStream();
            byte zero = 0;
            int h = 1;
            int h2 = 2 * h;

            for (int i = 0; i < _data.Length; i++){
                try{
                    _result.WriteByte((byte)derivative(_data,i));
                    //return ((double)_data[x + h] - (double)_data[x]) / h;
                }
                catch{
                    _result.WriteByte(zero);
                }
            }

            return _result.ToArray();
        }

        /*
        public TableData[] GetParametersFromExcersice(double[] _data){
            int _numRep = Repeticiones(_data);

        }
         */
         
        
        public void PrintGraph (ExcersiceSerieInformation _excersice){
            
            for (int i = 0; i < _excersice._speed.Length-1 ; i ++){
                this.PlotWindow.Series["Amplitud"].Points.AddXY(((double)i*110)/1000.0,(double) _excersice._amp[i]);
                //this.PlotWindow2.Series["Velocidad"].Points.AddXY((double)i,integral(_data,i, i+1, 20));
                //this.PlotWindow2.Series["Velocidad"].Points.AddXY((double)i, derivative(_data, i));
                this.PlotWindow2.Series["VelAmp"].Points.AddXY((double)_excersice._amp[i], _excersice._speed[i]);
                //this.PlotWindow3.Series["Test"].Points.AddXY((double)i, _segundaDerivada[i]);
                //this.PlotWindow3.Series["Potencia"].Points.AddXY(((double)i * 110) / 1000.0, _segundaDerivada[i] * 10);
                this.PlotWindow3.Series["Potencia"].Points.AddXY(((double)i * 110) / 1000.0, _excersice._acc[i]);
            }
         
            //this.PlotWindow.Series["Velocidad"].Points.AddXY(BitConverter.ToDouble(_data, 0), BitConverter.ToDouble(_data, 0));
            //this.PlotWindow.Series["Velocidad"].Points.AddY(BitConverter.ToSingle(_data, 0));
            this.PlotWindow.Series["Amplitud"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
            this.PlotWindow.Series["Amplitud"].Color = Color.Blue;
            this.PlotWindow.ChartAreas["ChartArea1"].Axes[0].Title = "Tiempo (s)";
            this.PlotWindow.ChartAreas["ChartArea1"].Axes[1].Title = "Amplitud (cm)";
            this.PlotWindow2.Series["VelAmp"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
            this.PlotWindow2.Series["VelAmp"].Color = Color.Red;
            this.PlotWindow2.ChartAreas["ChartArea1"].Axes[0].Title = "Amplitud (cm)";
            this.PlotWindow2.ChartAreas["ChartArea1"].Axes[1].Title = "Velocidad (cm/s)";
            // Curva de Potencia respecto al tiempo
            this.PlotWindow3.Series["Potencia"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine;
            this.PlotWindow3.Series["Potencia"].Color = Color.Green;
            this.PlotWindow3.ChartAreas["ChartArea1"].Axes[0].Title = "Tiempo (s)";
            this.PlotWindow3.ChartAreas["ChartArea1"].Axes[1].Title = "Potencia (W)";



            foreach (TableData _table in _excersice._seriesInfo){
                this.Table.Rows.Add(_excersice._seriesInfo.IndexOf(_table)+1, (int)_table._ampM, _table._time, (int)_table._speedM, _table._weight, (int)_table._powerM);
            }

            this.Table.Rows.Add(String.Format("TOTAL: {0}", _excersice._seriesInfo.Count), (int)_excersice._seriesTotal._ampM, _excersice._seriesTotal._time, (int)_excersice._seriesTotal._speedM, _excersice.weight, (int)_excersice._seriesTotal._powerM);
            
        }

        private void Table_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
  
}
